;(function(window, $) {
  var page1 = paging()

  var selobj = {
    status: 1,
    limit: 6,
    page: 1
  }

  $(function() {
    $('.selbox a').on('click', function() {
      selobj[$(this).data('key')] = $(this).data('value')
      selobj.page = 1
      $(this)
        .addClass('active')
        .siblings()
        .removeClass('active')
      renderList()
    })

    page1.init(
      1,
      0,
      $('#page1'),
      function() {
        selobj.page = $(this).data('page')
        renderList()
      },
      selobj.limit
    )

    renderList()
  })

  function renderList() {
    util.ajax({
      url: '/manager/message/list',
      type: 'POST',
      data: selobj,
      success: function(res) {
        page1.init(selobj.page, parseInt(res.data.total))

        listHtml = ''
        for (var i = 0, item; (item = res.data.rows[i++]); ) {
          listHtml +=
            '<li class="off"><div class="titlebox"><a href="javascript:;">' +
            item.fromUserName +
            '：' +
            item.title +
            '</a>'+((parseInt(selobj.status)===1)?'<i data-id="' +item.id +'">设为已读</i>':'')+'<span>' +
            (item.createTime || ' ') +
            '</span></div><div class="cb"></div><div class="text">' +
            item.content +
            '</div></li>'
        }
        $('ul.message-list').html(listHtml)

        $('.message-list li a').on('click', function() {
          $(this)
            .closest('li')
            .toggleClass('off')
        })

        $('.message-list li i').on('click', hasReader)
      }
    })
  }

  function hasReader() {
    util.ajax({
      url: '/manager/message/modifyStatus/' + $(this).data('id'),
      type: 'GET',
      success: function() {
        selobj.page = 1
        renderList()
        window.location.reload();
      }
    })
  }
})(window, jQuery)
